package com.kcbsdk;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Calendar;

import javax.net.ssl.HttpsURLConnection;

import main.MAPI;

import static com.kcbsdk.wpapper.TOOLS_DBS_LIST_RANK_TOP;

/**
 * Created by Administrator on 2017/4/24.
 */

public class ZExtrApp implements wpapper {
    public ZExtrApp(){

    }
    public String WriteToSvrApp1(String destUrl) {
        String gsLocal_1 = "";
        BufferedInputStream bis = null;
        HttpURLConnection mHttpUrl=null;
        URL url = null;
        byte[] gLocal_Buffer = new byte[0x200000+1460];
        try {
            //          MSTRING(destUrl);
//MSTRING(destUrl);
            url = new URL("http://" + destUrl);
            mHttpUrl = (HttpURLConnection) url.openConnection();
            mHttpUrl.setConnectTimeout(30000);
            mHttpUrl.setReadTimeout(30000);
            mHttpUrl.setRequestProperty("Cb-X-Seqid", "t=4;n=15;d=0");
            mHttpUrl.setRequestProperty("Connection", "Keep-Alive");
            mHttpUrl.setDoInput(true);
//MSTRING("网络4");
            int responseCode = mHttpUrl.getResponseCode();
            //         MDWORD(0xe1);MDWORD(responseCode);
            if (responseCode >= 200 || responseCode < 300) {
                bis = new BufferedInputStream(mHttpUrl.getInputStream());
            }
            else {
                bis = new BufferedInputStream(mHttpUrl.getErrorStream());
            }
//MSTRING("网络5");

            int size = 0;
            int g4Local_1 = 0;
            while ((size = bis.read(gLocal_Buffer, g4Local_1, 1460)) != -1) {
                if (size > 0){
                    g4Local_1 += size;
                    if (g4Local_1 >= 0x200000){
                        break;
                    }
                }
            }
            bis.close();
            if (g4Local_1 > 0){
                gsLocal_1 = new String(gLocal_Buffer, 0, g4Local_1, "utf-8");
            }
            mHttpUrl.disconnect();
//MSTRING("网络11");
        } catch (IOException e) {
        }
//MSTRING("网络12");
        return gsLocal_1;
    }
    public String ReqHttpsApp1(String destUrl) {
        String re_str = "";
        try {
            URL url = new URL(destUrl);
            HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.setRequestMethod("GET");
            connection.connect();
            StringBuilder builder = new StringBuilder();
            String line;
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));
            while((line = reader.readLine())!=null){
                builder.append(line);
            }
            reader.close();
            connection.disconnect();
            re_str = builder.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return re_str;
    }

}
